package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.umakr.ax.common.mapper.RadioStationMapper;
import com.umakr.ax.common.model.RadioStation;
import com.umakr.ax.common.vo.AreaRsVO;
import com.umakr.ax.common.vo.RadioBaseInfoVO;
import com.umakr.ax.common.vo.RadioStationVO;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import com.umakr.ax.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 电台信息 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class RadioStationService extends BaseService<RadioStationMapper, RadioStation>{

    @Autowired
    AreaRsService areaRsService;

    @Autowired
    Environment env;

    private static final String OSS_IMG_PATH = "oss.img.path";

    /**
     *
     * @param type 0 推荐电台 1 用户关注电台
     * @param userId 用户id
     * @param areaCode 地区id
     * @return
     */
    public Object listRadio(Integer type ,Long userId ,String areaCode ){

        if((type != 0 && type != 1)  ) {
            return resultMap(0,"参数错误");
        }
        if(userId == null && type == 1) {
            return WebUtil.getPage();
        }
        String aliUrl = env.getProperty(OSS_IMG_PATH);
        Page<RadioBaseInfoVO> page = baseMapper.getRadioPageByTypeAndUserIdAndAreaCode(type,userId,areaCode);
        for(RadioBaseInfoVO vo : page.getRecords()){
            vo.setAccountQrcodeUrl(FormatPhotoUrlUtil.formatString(aliUrl,vo.getAccountQrcodeUrl()));
            //修改电台logo
            formatBaseInfoVo(vo);
            StringBuilder jsonRsFm = new StringBuilder("[");
            String rsFm = "";
            List<AreaRsVO> listAreaRs = areaRsService.listAreaRs(vo.getRsId());
            if(listAreaRs != null){
                Integer index = 0;
                for (AreaRsVO areaRsVO : listAreaRs){
                    rsFm = "{\"index\":"+index+",\"areaName\":\""+areaRsVO.getAreaName()+"\",\"rsFm\":\""+areaRsVO.getRsFm()+"\"},";
                    jsonRsFm.append(rsFm);
                    index ++ ;
                }
            }
            jsonRsFm = new StringBuilder(jsonRsFm.substring(0,jsonRsFm.length()-1));
            jsonRsFm.append("]");
            vo.setJsonRsFm(jsonRsFm.toString());
        }

        return page;
    }
    public Object getRadioInfoByUserId(Long userId ,Long rsId, String areaCode){
        String aliUrl = env.getProperty(OSS_IMG_PATH);
        RadioBaseInfoVO vo = baseMapper.getRadioInfoByUserIdAndRsId(userId,rsId,areaCode);

        if(vo != null){
            vo.setAccountQrcodeUrl(FormatPhotoUrlUtil.formatString(aliUrl,vo.getAccountQrcodeUrl()));
            //修改电台logo
            formatBaseInfoVo(vo);
            //设置电台落地频率
            StringBuilder jsonRsFm = new StringBuilder("[");
            String rsFm = "";
            List<AreaRsVO> listAreaRs = areaRsService.listAreaRs(vo.getRsId());
            if(listAreaRs != null){
                Integer index = 0;
                for (AreaRsVO areaRsVO : listAreaRs){
                    rsFm = "{\"index\":"+index+",\"areaName\":\""+areaRsVO.getAreaName()+"\",\"rsFm\":\""+areaRsVO.getRsFm()+"\"},";
                    jsonRsFm.append(rsFm);
                    index ++ ;
                }
            }
            jsonRsFm = new StringBuilder(jsonRsFm.substring(0,jsonRsFm.length()-1));
            jsonRsFm.append("]");
            vo.setJsonRsFm(jsonRsFm.toString());

            return resultMap(vo);
        }
        return  resultMap("未查询到电台信息");
    }


    public Object getRadioInfoByRsCode(String rsCode ){
        if(rsCode != null){
            rsCode = rsCode.replace("\"","");
            RadioBaseInfoVO vo = baseMapper.getRadioInfoByRsCode(rsCode);
            if(vo != null){
                //修改电台logo
                formatBaseInfoVo(vo);

                return resultMap(vo);
            }

            return resultMap("未查询到 电台信息");
        }

        return resultMap("参数不能为空");
    }

    private void formatBaseInfoVo(RadioBaseInfoVO vo){
        String aliUrl = env.getProperty(OSS_IMG_PATH);
        vo.setRsLogo(aliUrl+vo.getRsLogo());
        if(vo.getFollowFrom() != null) {
            String fromName = "";
            switch (vo.getFollowFrom()) {
                case 0:
                    fromName = "聊天时自动关注";
                    break;
                case 1:
                    fromName = "电台列表手动关注";
                    break;
                case 2:
                    fromName = "摇活动时自动关注";
                    break;
                case 3:
                    fromName = "自动识别自动关注";
                    break;
                case 4:
                    fromName = "收藏电台秀时自动关注";
                    break;
                case 5:
                    fromName = "点赞电台秀时自动关注";
                    break;
                case 6:
                    fromName = "评论电台秀自动关注";
                    break;
                case 7:
                    fromName = "发表求助时自动关注";
                    break;
                    default:
            }
            vo.setFollowFromName(fromName);
        }

        String status = "";
        if(vo.getFollowStatus() != null) {
            status = "关注";
            vo.setFollowStatus(1);
        }else{
            status = "未关注";
            vo.setFollowStatus(0);
        }
        vo.setFollowStatusName(status);
    }

    public Page<RadioStationVO> listRadioStation(String rsName) {
        return baseMapper.listRadioStation(rsName);
    }

    public void updateStatusById(Long id,Integer status) {
        baseMapper.updateStatusById(id,status);
    }

    public void saveRadioStation(RadioStation radioStation) {
        baseMapper.saveRadioStation(radioStation);
    }

    public void updateRadioStation(RadioStation radioStation) {
        baseMapper.updateRadioStation(radioStation);
    }


    public Map<String, Object> getRsAdminByRsId(Long id) {
        return  baseMapper.getRsAdminByRsId(id);
    }
}
